package channelTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class FileChannelTest {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		File f=new File("/home/liulinhui/git/test/FileChannelTest.java");
		try {
			FileChannel infileChannel=new FileInputStream(f).getChannel();
			FileChannel outfileChannel=new FileOutputStream("/home/liulinhui/git/test/a.text").getChannel();
			{
				MappedByteBuffer mappedByteBuffer=infileChannel.map(FileChannel.MapMode.READ_ONLY,0,f.length());
				//创建解码器
				Charset charset=Charset.forName("utf-8");
				outfileChannel.write(mappedByteBuffer);
				mappedByteBuffer.clear();
				//创建解码器对象
				CharsetDecoder charsetDecoder=charset.newDecoder();
				//使用解码器将byteBuffer转成CharBuffer
				CharBuffer charBuffer=charsetDecoder.decode(mappedByteBuffer);
				System.out.println(charBuffer);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		}

}
